package com.ronan.common.response;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;
import java.util.Date;


/**
 * 响应
 *
 * @author ronan
 * @date 2021/02/25
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /** 是否成功 */
    private boolean success;

    /** 错误码 */
    private Integer code;

    /** 信息 */
    private String msg;

    /** 内容 */
    private T data;

    private String timestamp = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");

    public Result() {
        this.code = ResultCode.SUCCESS.getCode();
        this.msg = ResultCode.SUCCESS.getMsg();
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> Result<T> ok() {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
    }

    public static <T> Result<T> ok(T data) {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    public static <T> Result<T> okMsg(String msg) {
        return new Result(ResultCode.SUCCESS.getCode(), msg, null);
    }

    public static <T> Result<T> ok(T data, String msg) {
        return new Result(ResultCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> Result<T> failed() {
        return new Result(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMsg(), null);
    }

    public static <T> Result<T> failed(String msg) {
        return new Result(ResultCode.FAIL.getCode(), msg, null);
    }

    public static <T> Result<T> failed(int code, String msg) {
        return new Result(code, msg, null);
    }

    public static <T> Result<T> failedParam(String msg) {
        return new Result(ResultCode.FAIL_PARAM.getCode(), msg, null);
    }

    public static <T> Result<T> failedBiz(String msg) {
        return new Result(ResultCode.FAIL_BIZ.getCode(), msg, null);
    }

    public static <T> Result<T> failedSys(String msg) {
        return new Result(ResultCode.FAIL_SYS.getCode(), msg, null);
    }

    public static <T> Result<T> failedRpc() {
        return new Result(ResultCode.FAIL_RPC.getCode(), ResultCode.FAIL_RPC.getMsg(), null);
    }

    public static <T> Result<T> failedRpc(String msg) {
        return new Result(ResultCode.FAIL_RPC.getCode(), msg, null);
    }

    public static <T> Result<T> whether(boolean succFlag, String failedMsg) {
        return succFlag ? ok() : failedBiz(failedMsg);
    }

    public static <T> Result<T> whether(boolean succFlag, T data, String failedMsg) {
        return succFlag ? ok(data) : failedBiz(failedMsg);
    }

    public static <T> Result<T> whether(boolean succFlag, int code, String failedMsg) {
        return succFlag ? ok() : failed(code, failedMsg);
    }

    public static <T> Result<T> whether(boolean succFlag, T data, int code, String failedMsg) {
        return succFlag ? ok(data) : failed(code, failedMsg);
    }

    public static <T> Result<T> param(int code, String msg, T data) {
        return new Result(code, msg, data);
    }

    public static <T> Result<T> param(int code, String msg) {
        return new Result(code, msg);
    }

    public static <T> Result<T> param(Result<T> res) {
        return new Result(res.getCode(), res.getMsg(), res.getData());
    }

    @JsonIgnore
    public boolean isSucc() {
        return ResultCode.SUCCESS.getCode().equals(this.code);
    }

    @JsonIgnore
    public boolean isNotSucc() {
        return !this.isSucc();
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    @Override
    public String toString() {
        return "Result{" +
                "success=" + success +
                ", code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", timestamp='" + timestamp + '\'' +
                '}';
    }
}
